
#include "ql.h"
#include <stdlib.h>
#include <string.h>

#ifdef QL_DBG_MEM_IMPL

void * ql_dbg_mallocz_impl( size_t s, const char * file, int line )
{
    void * p = _malloc_dbg( s, _NORMAL_BLOCK, file, line );
    if( p ) memset( p, 0, s );
    return p;
}

void ql_dump_mem_leaks()
{
    _CrtDumpMemoryLeaks();
}

#else // !QL_DBG_MEM_IMPL

void * ql_malloc( AllocContext * c, size_t size )
{
    void * p = malloc( size );
    if( !p )
    {
        alloc_emit_error( c, QL_ERR_OUT_OF_MEMORY );
    }

    return p;
}

void * ql_mallocz( AllocContext * c, size_t size )
{
    void * p = malloc( size );
    if( !p )
    {
        alloc_emit_error( c, QL_ERR_OUT_OF_MEMORY );
    }

    memset( p, 0, size );
    return p;
}

void * ql_realloc( AllocContext * c, void * ptr, size_t size )
{
    void * p = realloc( ptr, size );
    if( !p && !size )
    {
        alloc_emit_error( c, QL_ERR_OUT_OF_MEMORY );
    }

    return p;
}

void ql_free( void * ptr )
{
    free( ptr );
}

#endif // !QL_DBG_MEM_IMPL
